﻿using AnesSystem.Controls;
using AnesSystem.Models;
using AnesSystem.Services;
using AnesSystem.Util;
using Common;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;

namespace AnesSystem.ViewModels
{
    public class OperationNoticeCtrlViewModel : BaseViewModel<OperationNoticeCtrlViewModel>
    {

        #region Variables

        private ObservableCollection<OperationNoticeItemCtrl> _displayOprList = new ObservableCollection<OperationNoticeItemCtrl>();
        private DateTime _selectDateTime = DateTime.Now;
        private OpeatonNoticeModel opeatonNotice = new OpeatonNoticeModel();
        private int itemCount = 30;
        #endregion

        #region Constructor
        public OperationNoticeCtrlViewModel()
        {
            InitHeaderPatient();
            _selectDateTime = DateTime.Now;
            UpdateDisplayOprList();
        }
        #endregion

        #region Properties
        public List<PatientLinkModel> GetPatientLink()
        {
            return PatientLinkService.Instance.SechudlePatientByTime(
                new DateTime(_selectDateTime.Year, _selectDateTime.Month, _selectDateTime.Day),
                new DateTime(_selectDateTime.Year, _selectDateTime.Month, _selectDateTime.Day).AddDays(1)); ;
        }

        public ObservableCollection<OperationNoticeItemCtrl> DisplayOprList
        {
            get
            {
                return _displayOprList;
            }

            set
            {
                _displayOprList = value;
                //RaisePropertyChanged("DisplayOprList");
            }
        }

        public DateTime SelectDateTime
        {
            get
            {
                return _selectDateTime;
            }

            set
            {
                _selectDateTime = value;
                UpdateDisplayOprList();
            }
        }


        #endregion

        #region 依赖属性

        #endregion

        #region Event

        #endregion

        #region Private Methods

        #endregion

        #region Public/Protected Methods


        public void UpdateDisplayOprList()
        {
            DisplayOprList.Clear();
            List<PatientLinkModel> list = PatientLinkService.Instance.SechudlePatientByTime(
                new DateTime(_selectDateTime.Year, _selectDateTime.Month, _selectDateTime.Day),
                new DateTime(_selectDateTime.Year, _selectDateTime.Month, _selectDateTime.Day).AddDays(1));
            //var tempDic = list.GroupBy(O => O.Schedule.PbtsInt).OrderBy(O => O.Key);
            var tempDic = list.GroupBy(O => O.Schedule.Pbssjh.ToDouble()).OrderBy(O => O.Key);
            List<OpeatonNoticeModel> lac = new List<OpeatonNoticeModel>();
            int index = 1;
            for (int i = 0; i < tempDic.Count(); i++)
            {
                var group = tempDic.ElementAt(i).OrderBy(O => O.Schedule.PbtsInt);

                foreach (var item in group)
                {
                    OpeatonNoticeModel model = new OpeatonNoticeModel()
                    {
                        Index = index.ToString(),
                        PatientLink = item
                    };
                    index++;
                    lac.Add(model);
                }
            }

            int itemsCount = lac.Count / itemCount;
            int remaining = lac.Count % itemCount;
            if (itemsCount >= 1)
            {
                for (int i = 0; i <= itemsCount; i++)
                {
                    OperationNoticeItemCtrl ctrl = new OperationNoticeItemCtrl();
                    if (i == itemsCount && remaining != 0)
                    {
                        ctrl.ItemsSource = lac.GetRange(i * itemCount, remaining);
                        for (int j = 0; j < (itemCount - remaining); j++)
                        {
                            OpeatonNoticeModel model = new OpeatonNoticeModel();
                            model.Index = (lac.Count + j + 1).ToString();
                            ctrl.ItemsSource.Add(model);
                        }
                        ctrl.ItemsSource.Insert(0, opeatonNotice);
                        DisplayOprList.Add(ctrl);
                        break;
                    }
                    ctrl.SetBinding(OperationNoticeItemCtrl.SelectDateTimeProperty, new Binding("SelectDateTime")
                    {
                        Source = this,
                        UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
                    });
                    ctrl.ItemsSource = lac.GetRange(i * itemCount, itemCount);
                    ctrl.ItemsSource.Insert(0, opeatonNotice);
                    DisplayOprList.Add(ctrl);
                }
            }
            else
            {
                lac.Insert(0, opeatonNotice);
                OperationNoticeItemCtrl ctrl = new OperationNoticeItemCtrl();
                for (int i = lac.Count; i < itemCount; i++)
                {
                    OpeatonNoticeModel model = new OpeatonNoticeModel();
                    model.Index = (i + 1).ToString();
                    lac.Add(model);
                }
                ctrl.ItemsSource = lac;
                DisplayOprList.Add(ctrl);
            }
        }
        public override void Refresh()
        {
            UpdateDisplayOprList();
        }

        public void InitHeaderPatient()
        {
            opeatonNotice.PatientLink = new PatientLinkModel();
            opeatonNotice.Index = "序号";
            opeatonNotice.Ssj = "手术间";
            opeatonNotice.Jhsj = "计划时间";
            opeatonNotice.Mzys = "麻醉医生";
            opeatonNotice.PatientLink.Patient.Ksmc = "科室";
            opeatonNotice.PatientLink.Patient.Zych = "床号";
            opeatonNotice.PatientLink.Patient.Brxm = "姓名";
            opeatonNotice.PatientLink.Patient.Xb = "性别";
            opeatonNotice.PatientLink.Patient.Nl = "年龄";
            opeatonNotice.PatientLink.Patient.Zyh = "住院号";
            opeatonNotice.PatientLink.Application.Nsssmc = "手术名称";
            opeatonNotice.PatientLink.Application.Ssysxm = "手术医生";
            opeatonNotice.PatientLink.Schedule.Mzfs = "麻醉方式";
            opeatonNotice.PatientLink.Schedule.Qxhsbh1 = "器械护士";
            opeatonNotice.PatientLink.Schedule.Xhhsbh1 = "巡回护士";
            opeatonNotice.PatientLink.Schedule.Bznr = "备注";
        }
        #endregion

        #region EventHandlers

        #endregion
    }

}
